package com.xuelang;

import com.alibaba.fastjson2.JSON;
import com.xuelang.pipeline.app.pipeline.PipelineService;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.pipeline.dto.*;
import com.xuelang.pipeline.client.pipeline.enums.TriggerWayEnum;
import com.xuelang.pipeline.client.pipeline.enums.YesOrNoEnum;
import com.xuelang.pipeline.client.task.dto.PipelineConfigTaskDTO;
import com.xuelang.pipeline.entry.pipeline.PipelineController;
import org.apache.commons.compress.utils.Lists;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import jakarta.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

/**
 * <p>流水线控制类单元测试</p>
 *
 * @author : yixin.lys
 * @date : 2024-06-13 16:22
 **/
@Transactional
@Rollback()
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Service
@ExtendWith(MockitoExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class PipelineControllerTest {

    @Autowired
    private PipelineService pipelineService;

    @Autowired
    private PipelineController pipelineController;

    private PipelineConfigDTO pipelineConfigDTO;
    private PipelineQuery pipelineQuery;
    private PageParam<PipelineQuery> pageParam;

    @BeforeEach
    public void setUp() {
        pipelineConfigDTO = new PipelineConfigDTO();
        pipelineQuery = new PipelineQuery();
        pageParam = new PageParam<>();
    }

    @DisplayName("测试流水线分页接口")
    @Test
    public void testPagePipelineList() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 目标数据初始化
        List<PipelineDTO> records = new ArrayList<>();
        records.add(PipelineDTO.builder()
                .code("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("JAVA流水线模板122")
                .projectCode("SYSTEM")
                .projectName(null)
                .environmentUuid("5b5542ea02bf40dc865aa5416fb4969f")
                .environmentName(null)
                .applicationCode("APP_002")
                .applicationName(null)
                .componentLabel("USER_CENTER_FRONT-jbovr")
                .tagUuidList(null)
                .tagNameList(null)
                .groupUuidList(null)
                .groupNameList(null)
                .contextInfo(null)
                .creatorId("13122222222")
                .createTime(dateFormat.parse("2024-06-06 14:13:17"))
                .build());

        PageRecord<PipelineDTO> targetRecord = new PageRecord<>(1L, 10L, 1L, 1L, records);


        // 初始化 PipelineQuery 并设置查询条件
        PipelineQuery query = new PipelineQuery();
        query.setCode("80e82e2a2d394d65a701ebb4d5e7689c");
        query.setCodeList(Arrays.asList("80e82e2a2d394d65a701ebb4d5e7689c"));
        query.setName("JAVA");
        query.setProjectCode("SYSTEM");
        query.setEnvironmentUuid("5b5542ea02bf40dc865aa5416fb4969f");
        query.setApplicationCode("APP_002");
        query.setTag(null);
        query.setGroup(null);
        query.setContextInfo(null);

        // 初始化 PageParam 并设置查询条件
        PageParam<PipelineQuery> pageParam = new PageParam<>();
        pageParam.setCurrentPage(1L);
        pageParam.setPageSize(10L);
        pageParam.setCondition(query);

        // 调用Controller方法
        ApiResult<PageRecord<PipelineDTO>> result = pipelineController.pagePipelineList(pageParam);

        // 断言
        assertNotNull(result);
        assertTrue(result.isSuccess());
        assertEquals(targetRecord, result.getData());
    }

    @DisplayName("测试根据条件查询流水线列表接口")
    @Test
    public void testConditionalPipelineList() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 目标数据初始化
        List<PipelineDTO> targetResult = new ArrayList<>();
        targetResult.add(PipelineDTO.builder()
                .code("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("JAVA流水线模板122")
                .projectCode("SYSTEM")
                .projectName(null)
                .environmentUuid("5b5542ea02bf40dc865aa5416fb4969f")
                .environmentName(null)
                .applicationCode("APP_002")
                .applicationName(null)
                .componentLabel("USER_CENTER_FRONT-jbovr")
                .tagUuidList(null)
                .tagNameList(null)
                .groupUuidList(null)
                .groupNameList(null)
                .contextInfo(null)
                .creatorId("13122222222")
                .createTime(dateFormat.parse("2024-06-06 14:13:17"))
                .build());


        // 初始化 PipelineQuery 并设置查询条件
        PipelineQuery query = new PipelineQuery();
        query.setCode("80e82e2a2d394d65a701ebb4d5e7689c");
        query.setCodeList(Arrays.asList("80e82e2a2d394d65a701ebb4d5e7689c"));
        query.setName("JAVA");
        query.setProjectCode("SYSTEM");
        query.setEnvironmentUuid("5b5542ea02bf40dc865aa5416fb4969f");
        query.setApplicationCode("APP_002");
        query.setTag(null);
        query.setGroup(null);
        query.setContextInfo(null);

        // 调用Controller方法
        ApiResult<List<PipelineDTO>> result = pipelineController.conditionalPipelineList(query);

        // 断言
        assertNotNull(result);
        assertTrue(result.isSuccess());
        assertEquals(targetResult, result.getData());
    }

    @DisplayName("新增流水线")
    @Test
    public void testAddPipeline() {
        // 初始化PipelineDTO
        PipelineDTO pipelineDTO = null;

        String pipelineCode = UUID.randomUUID().toString();

        pipelineDTO = PipelineDTO.builder()
                .code(pipelineCode)
                .name("测试新增流水线1")
                .projectCode("SYSTEM")
                .environmentUuid("5b5542ea02bf40dc865aa5416fb4969f")
                .applicationCode("APP_002")
                .componentLabel("USER_CENTER_FRONT-jbovr")
                .tagUuidList(null)  // Assuming tagUuidList is not provided in the SQL
                .tagNameList("红色标###红色标1")
                .groupUuidList(null)  // Assuming groupUuidList is not provided in the SQL
                .groupNameList(null)  // Assuming groupNameList is not provided in the SQL
                .contextInfo(null)
                .creatorId("13122222222")
                .createTime(new Date())
                .build();

        System.out.println(pipelineDTO);

        // 触发配置
        TriggerSettingDTO triggerSettingDTO = new TriggerSettingDTO();
        triggerSettingDTO.setPipelineCode(pipelineCode);
        triggerSettingDTO.setIsClockTrigger(YesOrNoEnum.YES.getValue());
        triggerSettingDTO.setIsCodeChangeTrigger(YesOrNoEnum.YES.getValue());
        // 单次触发
//        triggerSettingDTO.setTriggerWay(TriggerWayEnum.ONCE.getValue());
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 添加 5 分钟
        LocalDateTime fiveMinutesLater = now.plusMinutes(5);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String formattedFiveMinutesLater = fiveMinutesLater.format(formatter);
        triggerSettingDTO.setTriggerTime(formattedFiveMinutesLater);
        // 周期触发
        triggerSettingDTO.setTriggerWay(TriggerWayEnum.PERIOD.getValue());
        triggerSettingDTO.setWeekDay("MON");
        triggerSettingDTO.setPeriodTriggerTime(formattedFiveMinutesLater);

        pipelineConfigDTO.setIfRunPipeline(false);
        pipelineConfigDTO.setBaseInfo(pipelineDTO);
        pipelineConfigDTO.setTaskList(mockTaskDTOData(5));
        pipelineConfigDTO.setCacheSettingList(mockCacheSettingDTOData(5));
        pipelineConfigDTO.setVariableSettingList(mockVariableSettingDTOData(5));
        pipelineConfigDTO.setTriggerSetting(triggerSettingDTO);

        System.out.println(JSON.toJSONString(pipelineConfigDTO));

        ApiResult<Boolean> result = pipelineController.addPipeline(pipelineConfigDTO);

        assertNotNull(result);
        assertTrue(result.isSuccess());
        assertTrue(result.getData());
    }

    @DisplayName("更新流水线配置")
    @Test
    public void testUpdatePipeline() {
        // 初始化PipelineDTO
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        PipelineDTO pipelineDTO = null;

        String pipelineCode = "80e82e2a2d394d65a701ebb4d5e7689c";

        try {
            pipelineDTO = PipelineDTO.builder()
                    .code(pipelineCode)
                    .name("JAVA流水线模板1")
                    .projectCode("SYSTEM")
                    .environmentUuid("5b5542ea02bf40dc865aa5416fb4969f")
                    .applicationCode("APP_002")
                    .componentLabel("USER_CENTER_FRONT-jbovr")
                    .tagUuidList(null)  // Assuming tagUuidList is not provided in the SQL
                    .tagNameList("红色标###红色标1")
                    .groupUuidList(null)  // Assuming groupUuidList is not provided in the SQL
                    .groupNameList(null)  // Assuming groupNameList is not provided in the SQL
                    .contextInfo(null)
                    .creatorId("13122222222")
                    .createTime(sdf.parse("2024-06-06 14:13:17.400"))
                    .build();

            System.out.println(pipelineDTO);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // 触发配置
        TriggerSettingDTO triggerSettingDTO = new TriggerSettingDTO();
        triggerSettingDTO.setPipelineCode(pipelineCode);
        triggerSettingDTO.setIsClockTrigger(YesOrNoEnum.YES.getValue());
        triggerSettingDTO.setIsCodeChangeTrigger(YesOrNoEnum.YES.getValue());
        // 单次触发
//        triggerSettingDTO.setTriggerWay(TriggerWayEnum.ONCE.getValue());
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 添加 5 分钟
        LocalDateTime fiveMinutesLater = now.plusMinutes(5);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String formattedFiveMinutesLater = fiveMinutesLater.format(formatter);
        triggerSettingDTO.setTriggerTime(formattedFiveMinutesLater);
        // 周期触发
        triggerSettingDTO.setTriggerWay(TriggerWayEnum.PERIOD.getValue());
        triggerSettingDTO.setWeekDay("MON");
        triggerSettingDTO.setPeriodTriggerTime(formattedFiveMinutesLater);

        pipelineConfigDTO.setIfRunPipeline(false);
        pipelineConfigDTO.setBaseInfo(pipelineDTO);
        pipelineConfigDTO.setTaskList(mockTaskDTOData(5));
        pipelineConfigDTO.setCacheSettingList(mockCacheSettingDTOData(5));
        pipelineConfigDTO.setVariableSettingList(mockVariableSettingDTOData(5));
        pipelineConfigDTO.setTriggerSetting(triggerSettingDTO);

        System.out.println(JSON.toJSONString(pipelineConfigDTO));

        ApiResult<Boolean> result = pipelineController.updatePipeline(pipelineConfigDTO);

        assertNotNull(result);
        assertTrue(result.isSuccess());
        assertTrue(result.getData());
    }

    @DisplayName("测试删除流水线")
    @Test
    public void testDeletePipeline() {
        // 初始化PipelineDTO
        PipelineDTO pipelineDTO = null;

        String pipelineCode = UUID.randomUUID().toString();

        pipelineDTO = PipelineDTO.builder()
                .code(pipelineCode)
                .name("测试新增流水线1")
                .projectCode("SYSTEM")
                .environmentUuid("5b5542ea02bf40dc865aa5416fb4969f")
                .applicationCode("APP_002")
                .componentLabel("USER_CENTER_FRONT-jbovr")
                .tagUuidList(null)  // Assuming tagUuidList is not provided in the SQL
                .tagNameList("红色标###红色标1")
                .groupUuidList(null)  // Assuming groupUuidList is not provided in the SQL
                .groupNameList(null)  // Assuming groupNameList is not provided in the SQL
                .contextInfo(null)
                .creatorId("13122222222")
                .createTime(new Date())
                .build();

        System.out.println(pipelineDTO);

        // 触发配置
        TriggerSettingDTO triggerSettingDTO = new TriggerSettingDTO();
        triggerSettingDTO.setPipelineCode(pipelineCode);
        triggerSettingDTO.setIsClockTrigger(YesOrNoEnum.YES.getValue());
        triggerSettingDTO.setIsCodeChangeTrigger(YesOrNoEnum.YES.getValue());
        // 单次触发
//        triggerSettingDTO.setTriggerWay(TriggerWayEnum.ONCE.getValue());
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 添加 5 分钟
        LocalDateTime fiveMinutesLater = now.plusMinutes(5);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String formattedFiveMinutesLater = fiveMinutesLater.format(formatter);
        triggerSettingDTO.setTriggerTime(formattedFiveMinutesLater);
        // 周期触发
        triggerSettingDTO.setTriggerWay(TriggerWayEnum.PERIOD.getValue());
        triggerSettingDTO.setWeekDay("MON");
        triggerSettingDTO.setPeriodTriggerTime(formattedFiveMinutesLater);

        pipelineConfigDTO.setIfRunPipeline(false);
        pipelineConfigDTO.setBaseInfo(pipelineDTO);
        pipelineConfigDTO.setTaskList(mockTaskDTOData(5));
        pipelineConfigDTO.setCacheSettingList(mockCacheSettingDTOData(5));
        pipelineConfigDTO.setVariableSettingList(mockVariableSettingDTOData(5));
        pipelineConfigDTO.setTriggerSetting(triggerSettingDTO);

        System.out.println(JSON.toJSONString(pipelineConfigDTO));

        ApiResult<Boolean> addPipeline = pipelineController.addPipeline(pipelineConfigDTO);

        ApiResult<Boolean> deleteResult = pipelineController.deletePipeline(pipelineCode);

        ApiResult<PipelineDTO> result = pipelineController.queryPipeline(pipelineCode);
        assertNotNull(deleteResult);
        assertTrue(deleteResult.isSuccess());
        assertTrue(deleteResult.getData());
        assertNull(result.getData());
    }

    @DisplayName("测试根据流水线编码查询流水线")
    @Test
    public void testQueryPipeline() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 目标数据
        PipelineDTO targetPipeline = PipelineDTO.builder()
                .code("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("JAVA流水线模板122")
                .projectCode("SYSTEM")
                .projectName("系统内建项目")
                .environmentUuid("5b5542ea02bf40dc865aa5416fb4969f")
                .environmentName("测试环境")
                .applicationCode("APP_002")
                .applicationName("语音转写应用")
                .componentLabel("USER_CENTER_FRONT-jbovr")
                .tagUuidList("bc8d8682770a4bfd82443205cdf64e2f,16f5fa1d8ac94679a6655efe996af173")
                .tagNameList("INPORT,PINK")
                .groupUuidList("c2da8b9f94d945bebae8f36987448c1c")
                .groupNameList("min小组")
                .contextInfo(null)
                .creatorId("13122222222")
                .createTime(dateFormat.parse("2024-06-06 14:13:17"))
                .build();

        ApiResult<PipelineDTO> result = pipelineController.queryPipeline("80e82e2a2d394d65a701ebb4d5e7689c");

        assertNotNull(result);
        assertTrue(result.isSuccess());
        assertEquals(targetPipeline, result.getData());
    }

    @DisplayName("测试查询组件流水线信息")
    @Test
    public void testQueryComponentPipeline() {
        // 目标数据
        ComponentPipelineDTO targetResult = new ComponentPipelineDTO();
        targetResult.setPipelineCode("80e82e2a2d394d65a701ebb4d5e7689c");
        targetResult.setPipelineName("JAVA流水线模板122");

        // 初始化 PipelineQuery 并设置查询条件
        ComponentPipelineQuery query = new ComponentPipelineQuery();
        query.setEnvironmentUuid("5b5542ea02bf40dc865aa5416fb4969f");
        query.setComponentLabel("USER_CENTER_FRONT-jbovr");

        ApiResult<ComponentPipelineDTO> componentPipeline = pipelineController.queryComponentPipeline(null, query);

        assertNotNull(componentPipeline);
        assertTrue(componentPipeline.isSuccess());
        assertEquals(targetResult, componentPipeline.getData());
    }

    @DisplayName("测试获取流水线配置信息")
    @Test
    public void testQueryPipelineConfig() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 目标数据
        PipelineConfigDTO targetResult = new PipelineConfigDTO();
        targetResult.setBaseInfo(PipelineDTO.builder()
                .code("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("JAVA流水线模板122")
                .projectCode("SYSTEM")
                .projectName("系统内建项目")
                .environmentUuid("5b5542ea02bf40dc865aa5416fb4969f")
                .environmentName("测试环境")
                .applicationCode("APP_002")
                .applicationName("语音转写应用")
                .componentLabel("USER_CENTER_FRONT-jbovr")
                .tagUuidList("bc8d8682770a4bfd82443205cdf64e2f,16f5fa1d8ac94679a6655efe996af173")
                .tagNameList("INPORT,PINK")
                .groupUuidList("c2da8b9f94d945bebae8f36987448c1c")
                .groupNameList("min小组")
                .contextInfo(null)
                .creatorId("13122222222")
                .createTime(dateFormat.parse("2024-06-06 14:13:17"))
                .build());

        List<PipelineConfigTaskDTO> taskDTOList = Lists.newArrayList();
        PipelineConfigTaskDTO task1 = PipelineConfigTaskDTO.builder()
                .id(531L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .taskDefUuid("7cd37917d55b43f8a8f46b0d48ca9dc2")
                .uuid("OdOsRYdukGjSYqoX0y779")
                .parentUuid("2jRdbEOC6NvqD1MhRUDOW")
                .name("JAVA构建")
                .code("java_build")
                .params("{\"buildCommand\":\"mvn\"}")
                .sortIndex(1)
                .schema("[{colProps:{span:24,},component:'CodeInput',defaultValue:'/usr/bin/mvn package',field:'buildCommand',label:'构建命令',labelWidth:80,required:true,}]")
                .creatorId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .createTime(dateFormat.parse("2024-06-21 10:51:19"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-06-21 10:51:19"))
                .build();
        taskDTOList.add(task1);

        PipelineConfigTaskDTO task2 = PipelineConfigTaskDTO.builder()
                .id(532L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .taskDefUuid("f3c0032044694f12b2a649093f52667a")
                .uuid("2jRdbEOC6NvqD1MhRUDOW")
                .parentUuid("0UYVvI6Xr5-xP0NhS7Tib")
                .name("PYTHON代码扫描")
                .code("python_scan")
                .params("{\"codeScan\":\"./\",\"alertInfo\":[{\"identifier\":\"bugs\",\"type\":\"LE\",\"threshold\":22}]}")
                .sortIndex(1)
                .schema("[{colProps:{span:24,},component:'Input',defaultValue:'./',field:'codeScan',label:'扫描路径',labelWidth:80,required:true,},{colProps:{span:24,},component:'AlertInfo',field:'alertInfo',label:'红线信息',labelWidth:80,componentProps:{api:'/devops/task/redline/dict',fieldNames:{value:'id',label:'text'},},},]")
                .creatorId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .createTime(dateFormat.parse("2024-06-19 10:59:46"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-06-19 10:59:46"))
                .build();
        taskDTOList.add(task2);

        PipelineConfigTaskDTO task3 = PipelineConfigTaskDTO.builder()
                .id(533L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .taskDefUuid("b7a1fa4e847d4eae8b3b21e2368e2622")
                .uuid("0UYVvI6Xr5-xP0NhS7Tib")
                .parentUuid("Mv02KQj_H8HZeP1f6ilyD")
                .name("NODEJS构建")
                .code("nodejs_build")
                .params("{\"buildCommand\":\"npm\"}")
                .sortIndex(1)
                .schema("[{colProps:{span:24,},component:'CodeInput',defaultValue:'npm install && npm run build',field:'buildCommand',label:'构建命令',labelWidth:80,required:true,}]")
                .creatorId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .createTime(dateFormat.parse("2024-06-18 18:47:46"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-06-18 18:47:46"))
                .build();
        taskDTOList.add(task3);

        PipelineConfigTaskDTO task4 = PipelineConfigTaskDTO.builder()
                .id(534L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .taskDefUuid("55262349e02c4c2a897cf7c113c266a7")
                .uuid("Mv02KQj_H8HZeP1f6ilyD")
                .parentUuid("0")
                .name("代码源")
                .code("code_source")
                .params("{\"componentLabel\":\"USER_CENTER_FRONT-jbovr\",\"codeRepository\":\"https://gitlab.xuelangyun.com/appDev/rdp-test.git\",\"token\":\"gAvYKYeVDhxGt9sLtDfv\",\"branch\":\"feature/12_20230113_01\"}")
                .sortIndex(1)
                .schema("[{field:'componentLabel',label:'',component:'Input',vShow:false,},{field:'codeRepository',label:'代码仓库',component:'RichSelect',colProps:{span:24,},labelWidth:80,componentProps:({formModel})=>({placeholder:'请选择代码仓库',optionMode:'api',params:{componentLabel:formModel.componentLabel},api:'/devops/component/repo/dict',fieldNames:{value:'id',label:'text',},}),required:true,dynamicRules:()=>[{required:true,message:'请选择代码仓库'}],},{colProps:{span:24,},component:'RichSelect',field:'token',label:'token',componentProps:({formModel})=>({placeholder:'请选择token',optionMode:'api',params:{componentLabel:formModel.componentLabel},api:'/devops/component/token/dict',fieldNames:{value:'id',label:'text',},}),labelWidth:80,required:true,dynamicRules:()=>[{required:true,message:'请选择token'}],},{colProps:{span:24,},component:'RichSelect',field:'branch',label:'分支',componentProps:({formModel})=>({placeholder:'请选择分支',optionMode:'api',params:{codeRepository:formModel.codeRepository,token:formModel.token},api:'/devops/git/remote/branch/list/dict',showSearch:true,isBackendSearch:true,fieldNames:{value:'id',label:'text',},}),labelWidth:80,required:true,dynamicRules:()=>[{required:true,message:'请选择分支'}],}]")
                .creatorId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .createTime(dateFormat.parse("2024-06-18 18:47:46"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-06-18 18:47:46"))
                .build();
        taskDTOList.add(task4);

        targetResult.setTaskList(taskDTOList);

        targetResult.setTriggerSetting(TriggerSettingDTO.builder()
                .id(4L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .isCodeTrigger(1)
                .isClockTrigger(1)
                .triggerWay(1)
                .triggerTime("2024-06-20 11:25:52")
                .weekDay("WED")
                .periodTriggerTime("21:09:05")
                .cronExpression("05 09 21 ? * WED")
                .isCodeChangeTrigger(1)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 11:21:48"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build());

        List<VariableSettingDTO> variableSettingList = Lists.newArrayList();
        VariableSettingDTO var1 = VariableSettingDTO.builder()
                .id(106L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("测试变量1")
                .defaultValue("测试变量1")
                .isCommon(0)
                .isPrivate(0)
                .isRunSetting(0)
                .isRunSelect(0)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:57"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        variableSettingList.add(var1);

        VariableSettingDTO var2 = VariableSettingDTO.builder()
                .id(107L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("测试变量2")
                .defaultValue("测试变量2")
                .isCommon(0)
                .isPrivate(0)
                .isRunSetting(0)
                .isRunSelect(0)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:57"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        variableSettingList.add(var2);

        VariableSettingDTO var3 = VariableSettingDTO.builder()
                .id(108L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("测试变量3")
                .defaultValue("测试变量3")
                .isCommon(0)
                .isPrivate(0)
                .isRunSetting(0)
                .isRunSelect(0)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:57"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        variableSettingList.add(var3);

        VariableSettingDTO var4 = VariableSettingDTO.builder()
                .id(109L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("测试变量4")
                .defaultValue("测试变量4")
                .isCommon(0)
                .isPrivate(0)
                .isRunSetting(0)
                .isRunSelect(0)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:57"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        variableSettingList.add(var4);

        VariableSettingDTO var5 = VariableSettingDTO.builder()
                .id(110L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("测试变量5")
                .defaultValue("测试变量5")
                .isCommon(0)
                .isPrivate(0)
                .isRunSetting(0)
                .isRunSelect(0)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:57"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        variableSettingList.add(var5);

        VariableSettingDTO var6 = VariableSettingDTO.builder()
                .id(116L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("123")
                .defaultValue("333")
                .isCommon(0)
                .isPrivate(0)
                .isRunSetting(0)
                .option("")
                .isRunSelect(0)
                .creatorId("48ab74734cb24cb0b5656a91b80b2fb7")
                .createTime(dateFormat.parse("2024-06-18 17:59:23"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        variableSettingList.add(var6);

        VariableSettingDTO var7 = VariableSettingDTO.builder()
                .id(117L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .name("运行变量")
                .defaultValue("22")
                .isCommon(0)
                .isPrivate(0)
                .isRunSetting(0)
                .option("22")
                .isRunSelect(1)
                .creatorId("48ab74734cb24cb0b5656a91b80b2fb7")
                .createTime(dateFormat.parse("2024-06-18 18:00:20"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        variableSettingList.add(var7);

        targetResult.setVariableSettingList(variableSettingList);

        List<CacheSettingDTO> cacheSettingDTOList = Lists.newArrayList();
        CacheSettingDTO cache1 = CacheSettingDTO.builder()
                .id(116L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .directory("/root/1")
                .description("测试缓存1")
                .isOpen(0)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:56"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        cacheSettingDTOList.add(cache1);

        CacheSettingDTO cache2 = CacheSettingDTO.builder()
                .id(117L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .directory("/root/2")
                .description("测试缓存2")
                .isOpen(1)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:56"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        cacheSettingDTOList.add(cache2);

        CacheSettingDTO cache3 = CacheSettingDTO.builder()
                .id(118L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .directory("/root/3")
                .description("测试缓存3")
                .isOpen(1)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:56"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        cacheSettingDTOList.add(cache3);

        CacheSettingDTO cache4 = CacheSettingDTO.builder()
                .id(119L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .directory("/root/4")
                .description("测试缓存4")
                .isOpen(1)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:56"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        cacheSettingDTOList.add(cache4);

        CacheSettingDTO cache5 = CacheSettingDTO.builder()
                .id(120L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .directory("/root/5")
                .description("测试缓存5")
                .isOpen(1)
                .creatorId("f3d0174a4a2a434bbf7cbeecb161e60e")
                .createTime(dateFormat.parse("2024-06-17 18:30:56"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        cacheSettingDTOList.add(cache5);

        CacheSettingDTO cache6 = CacheSettingDTO.builder()
                .id(123L)
                .pipelineCode("80e82e2a2d394d65a701ebb4d5e7689c")
                .directory("../ssds")
                .description("czxc")
                .isOpen(0)
                .creatorId("48ab74734cb24cb0b5656a91b80b2fb7")
                .createTime(dateFormat.parse("2024-06-18 18:03:09"))
                .updaterId("c59ce2cfdb80418eb6d3a2980b2db0ce")
                .updateTime(dateFormat.parse("2024-07-12 17:47:48"))
                .build();
        cacheSettingDTOList.add(cache6);

        targetResult.setCacheSettingList(cacheSettingDTOList);

        ApiResult<PipelineConfigDTO> result = pipelineController.queryPipelineConfig("80e82e2a2d394d65a701ebb4d5e7689c");

        assertNotNull(result);
        assertTrue(result.isSuccess());
        assertEquals(targetResult, result.getData());
    }

    private List<PipelineConfigTaskDTO> mockTaskDTOData(int count) {
        List<PipelineConfigTaskDTO> taskList = new ArrayList<>();
        String[] taskDefUuids = {
                "55262349e02c4c2a897cf7c113c266a7",
                "7cd37917d55b43f8a8f46b0d48ca9dc2",
                "b7a1fa4e847d4eae8b3b21e2368e2622",
                "766531ce082748a7b647900699d11ace",
                "107fe8179ccc4dd6b8edcffca406c0ee",
                "c918414ec36c4f7bbcd9e6201caa8075",
                "3eab2c5bd7f34f26b7505cc8f755a772",
                "0d1ba23f5e204f76bef8e3a7c7bc562b",
                "f106aea2b0fe4d61b678fdc715664d6c",
                "fc84251b40e549c1aadede5c5fbf5636",
                "f3c0032044694f12b2a649093f52667a",
                "11090b5c0fb24f9fb182940a41b51087",
                "69b5f4f7330f459688d5b493af44fddf",
                "de28c78c411c430f8eb2b78142894d63",
                "adfa08dcaf8e42f096786e584079b7ea"
        };
        String pipelineCode = "80e82e2a2d394d65a701ebb4d5e7689c";
        Random random = new Random();

        String prevUuid = null;
        for (int i = 1; i <= count; i++) {
            String uuid = UUID.randomUUID().toString();
            String parentUuid = i == 1 ? "0" : prevUuid; // First task's parent UUID is "0"
            String taskDefUuid = taskDefUuids[random.nextInt(taskDefUuids.length)];
            String name = "Task Name " + i;
            String code = "Task Code " + i;
            String params = "Task Params " + i;
            String output = "Task Output " + i;
            int sortIndex = i;
            String creatorId = "creator_" + i;
            Date createTime = new Date();
            String updaterId = "updater_" + i;
            Date updateTime = new Date();

            PipelineConfigTaskDTO task = PipelineConfigTaskDTO.builder()
                    .pipelineCode(pipelineCode)
                    .taskDefUuid(taskDefUuid)
                    .uuid(uuid)
                    .parentUuid(parentUuid)
                    .name(name)
                    .code(code)
                    .params(params)
                    .output(output)
                    .sortIndex(sortIndex)
                    .creatorId(creatorId)
                    .createTime(createTime)
                    .updaterId(updaterId)
                    .updateTime(updateTime)
                    .build();

            taskList.add(task);
            prevUuid = uuid;
        }
        return taskList;
    }

    private List<CacheSettingDTO> mockCacheSettingDTOData(int count) {
        List<CacheSettingDTO> cacheSettingDTOList = new ArrayList<>();
        String pipelineCode = "80e82e2a2d394d65a701ebb4d5e7689c";

        for (int i = 1; i <= count; i++) {
            String description = "测试缓存" + i;
            String directory = "/root/" + i;
            Date createTime = new Date();
            Date updateTime = new Date();

            CacheSettingDTO cacheSettingDTO = CacheSettingDTO.builder()
                    .pipelineCode(pipelineCode)
                    .directory(directory)
                    .isOpen(YesOrNoEnum.YES.getValue())
                    .description(description)
                    .createTime(createTime)
                    .updateTime(updateTime)
                    .build();

            cacheSettingDTOList.add(cacheSettingDTO);
        }
        return cacheSettingDTOList;
    }

    private List<VariableSettingDTO> mockVariableSettingDTOData(int count) {
        List<VariableSettingDTO> variableSettingDTOList = new ArrayList<>();
        String pipelineCode = "80e82e2a2d394d65a701ebb4d5e7689c";

        for (int i = 1; i <= count; i++) {
            String defaultValue = "测试变量" + i;
            String name = "测试变量" + i;
            Date createTime = new Date();
            Date updateTime = new Date();

            VariableSettingDTO variableSettingDTO = VariableSettingDTO.builder()
                    .pipelineCode(pipelineCode)
                    .defaultValue(defaultValue)
                    .isPrivate(YesOrNoEnum.NO.getValue())
                    .isRunSetting(YesOrNoEnum.NO.getValue())
                    .isRunSelect(YesOrNoEnum.NO.getValue())
                    .name(name)
                    .createTime(createTime)
                    .updateTime(updateTime)
                    .build();

            variableSettingDTOList.add(variableSettingDTO);
        }
        return variableSettingDTOList;
    }

}
